﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Client_Web.ServiceReference;

namespace Client_Web.Gallery
{
    public partial class PictureAdd : System.Web.UI.Page
    {
        protected List<AlbumStruct> userAlbumList;

        protected string[] albumStringList;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (null != Session["userIdStr"])
            {
                AlbumSelect.Items.Add(new ListItem("Sélectionnez un album", "0"));
                ServiceClient serviceClient = new ServiceClient();
                albumStringList = serviceClient.getUserAlbum((string)Session["userIdStr"]);
                userAlbumList = new List<AlbumStruct>();
                for (int i = 1; i < albumStringList.Length; i++)
                {
                    string[] data = Util.CleanAndSplit(albumStringList[i]);
                    AlbumSelect.Items.Add(new ListItem(data[1], data[0]));
                }
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
        }

        protected void AddPicture_Click(object sender, EventArgs e)
        {
            if (PictureFile.HasFile)
            {
                try
                {
                    ServiceClient serviceClient = new ServiceClient();
                    string pictureId = serviceClient.addPicture(AlbumSelect.SelectedItem.Value, PictureName.Text, PictureFile.FileBytes);
                    Response.Redirect("~/Gallery/AlbumView.aspx?AlbumId=" + AlbumSelect.SelectedItem.Value);
                }
                catch (Exception exception)
                {
                    LabelError.Text = "Erreur : " + exception.Message.ToString();
                }
            }
            else
            {
                LabelError.Text = "Erreur : Aucun fichier spécifié.";
            }
        }
    }
}